<template>
  <div class="user-manage-container">
    <el-card class="page-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-icon class="header-icon"><UserFilled /></el-icon>
            <span class="header-title">员工管理</span>
          </div>
        </div>
      </template>
      
      <div v-if="userList.length === 0" class="empty-state">
        <el-empty description="暂无员工数据" />
      </div>
      
      <el-table 
        v-else
        :data="userList" 
        style="width: 100%"
        stripe
        :empty-text="'暂无员工数据'"
      >
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="username" label="用户名" width="140" align="center" />
        <el-table-column prop="name" label="姓名" width="120" align="center" />
        <el-table-column prop="role" label="角色" width="120" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.role === 'EMPLOYEE'" type="info" effect="dark">员工</el-tag>
            <el-tag v-else-if="scope.row.role === 'MANAGER'" type="success" effect="dark">经理</el-tag>
            <el-tag v-else-if="scope.row.role === 'HR'" type="warning" effect="dark">HR</el-tag>
            <el-tag v-else effect="dark">其他</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 'ACTIVE'" type="success">正常</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" align="center">
          <template #default="scope">
            <span class="time-text">{{ formatDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import request from '../utils/request'
import { ElMessage } from 'element-plus'
import { UserFilled } from '@element-plus/icons-vue'

export default {
  name: 'UserManage',
  components: {
    UserFilled
  },
  setup() {
    const userList = ref([])
    
    const formatDate = (dateStr) => {
      if (!dateStr) return ''
      return new Date(dateStr).toLocaleString('zh-CN')
    }
    
    const loadUserList = async () => {
      try {
        const res = await request.get('/user/list')
        userList.value = res.data || []
        if (userList.value.length === 0) {
          ElMessage.info('暂无员工数据')
        }
      } catch (error) {
        console.error('加载用户列表失败:', error)
        if (error.message && error.message.includes('404')) {
          ElMessage.error('用户服务未启动或未注册，请检查user-service是否正常运行')
        } else {
          ElMessage.error(error.message || '加载用户列表失败')
        }
      }
    }
    
    onMounted(() => {
      loadUserList()
    })
    
    return {
      userList,
      formatDate
    }
  }
}
</script>

<style scoped>
.user-manage-container {
  width: 100%;
  height: 100%;
}

.page-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 24px;
  color: #409eff;
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
}

.empty-state {
  padding: 60px 0;
}

.time-text {
  color: #8c8c8c;
  font-size: 14px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #fafafa;
  color: #333;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 16px 0;
}
</style>

